//
//  HomePageContentViewCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/29.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "HomePageContentViewCell.h"
#import "HomePageListPicView.h"
#import "HomePageContentModel.h"
@interface HomePageContentViewCell ()
@property (nonatomic,strong)UIButton *allBtn;
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)HomePageListPicView *picView;
@property (nonatomic,strong)HomePageItemModel *viewModel;
@end


@implementation HomePageContentViewCell


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
  
    self.backgroundColor = [UIColor whiteColor];
    
    self.contentLb = [[UILabel alloc]init];;
    self.contentLb.text = @"愿你们合上笔盖的刹那 有着战士收刀入鞘的骄傲";
    self.contentLb.textColor = C51;
    self.contentLb.numberOfLines = 5;
    self.contentLb.font = font(16);
    [self.contentView addSubview:self.contentLb];
    
    self.allBtn = [[UIButton alloc]init];
    [self.allBtn setTitle:@"全文" forState:UIControlStateNormal];
    [self.allBtn setTitleColor:C51 forState:UIControlStateNormal];
    self.allBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.allBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.allBtn addTarget:self action:@selector(allMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.allBtn];
    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    [self.allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(16);
        make.top.equalTo(self.contentLb.mas_bottom).offset(4);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    self.picView = [[HomePageListPicView alloc]init];
    [self.contentView addSubview:self.picView];
    [self.picView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.allBtn.mas_bottom).offset(16);
        make.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(0);
    }];
    __weak typeof(self)weakSelf = self;
    self.picView.didImageAtIndex = ^(NSInteger index) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(showImageAtIndex:)]) {
            [weakSelf.delegate showImageAtIndex:index];
        }
    };
}

- (void)bindViewModel:(HomePageItemModel *)viewModel {
    self.viewModel = viewModel;
    
    self.allBtn.hidden = YES;
    
    if (viewModel.contentType.intValue == 1) {
        self.picView.hidden = YES;
        // 判断行数是否>5
        NSInteger index = [self rowsOfString:viewModel.contentText withFont:font(16) withWidth:kScreenW - 32];
        // 显示全部按钮
        if (index >= 5) {
            [self.allBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentLb.mas_bottom).offset(4);
                make.size.mas_equalTo(CGSizeMake(100, 20));
            }];
            self.allBtn.hidden = NO;
        // 隐藏全部按钮
        }else{
            [self.allBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentLb.mas_bottom).offset(0);
                make.size.mas_equalTo(CGSizeZero);
            }];
            self.allBtn.hidden = YES;
        }
        
        [self.picView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        // 判断内容有没有空
        CGFloat textH = 16;
        if (viewModel.contentText.length == 0) {
            textH = 0;
            [self.allBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentLb.mas_bottom).offset(0);
                make.size.mas_equalTo(CGSizeZero);
            }];
            
        }else{
            NSInteger index = [self rowsOfString:viewModel.contentText withFont:font(16) withWidth:kScreenW - 32];
            if (index >= 5) {
                self.allBtn.hidden = NO;
                [self.allBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.contentLb.mas_bottom).offset(4);
                    make.size.mas_equalTo(CGSizeMake(100, 20));
                }];
            }else{
                self.allBtn.hidden = YES;
                [self.allBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.contentLb.mas_bottom).offset(0);
                    make.size.mas_equalTo(CGSizeZero);
                }];
            }
        }
        [self.picView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.allBtn.mas_bottom).offset(textH);
            make.height.mas_equalTo(viewModel.imageHeight);
        }];
    }
    self.contentLb.text = viewModel.contentText;
    
    if(viewModel.contentType.intValue == 2){
        self.picView.hidden = NO;
        NSMutableArray *urls = [NSMutableArray array];
        [viewModel.contents enumerateObjectsUsingBlock:^(HomePageContentModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [urls addObject:obj.postUrl.length==0?@"":obj.postUrl];
        }];
        [self.picView setIndex: @(viewModel.contents.count) urls:urls];
    }
}



-(NSInteger)rowsOfString:(NSString *)text withFont:(UIFont *)font withWidth:(CGFloat)width{
    if (!text || text.length == 0) {
        return 0;
    }
    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];
    CFRelease(myFont);
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,width,MAXFLOAT));
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
    return lines.count;
}

- (void)allMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showAllContent)]) {
        [self.delegate showAllContent];
    }
}

@end
